package com.ourteam.product.requirement.service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.ArrayUtils;

import com.ourteam.product.requirement.dao.IProductIssueDAO;
import com.ourteam.product.requirement.dao.ProductIssue;
import com.ourteam.product.requirement.dao.ProductIssueAttach;
import com.ourteam.product.requirement.dao.ProductIssueAttachDAOQueryBean;
import com.ourteam.product.requirement.dao.ProductIssueChangeLog;
import com.ourteam.product.requirement.dao.ProductIssueChangeLogDAOQueryBean;
import com.ourteam.product.requirement.dao.ProductIssueDAOQueryBean;
import com.ourteam.product.requirement.dao.ProductIssueNote;
import com.ourteam.product.requirement.dao.ProductIssueNoteDAOQueryBean;
import com.ourteam.product.requirement.domain.ProductIssueAttachBean;
import com.ourteam.product.requirement.domain.ProductIssueBean;
import com.ourteam.product.requirement.domain.ProductIssueChangeLogBean;
import com.ourteam.product.requirement.domain.ProductIssueNoteBean;

/**
 *
 * Template Name : 业务服务实现 Template Version : 1 Template Last Modfiy Date : Thu
 * Nov 23 10:43:58 CST 2017 Auto Generate Date : 2018-04-18 11:24:06
 *
 */
public class ProductIssueServiceImpl extends AbstractProductIssueService {
	/**
	 * $method.remarks
	 * 
	 * @param aProductIssue
	 * @throws Exception
	 */
	protected void doAddNewProductIssue(final com.ourteam.product.requirement.domain.ProductIssueBean aProductIssue)
			throws Exception {
		String issueCode = this.systemSerialCodeService.getSerialCode("product_issue");
		aProductIssue.setIssueCode(issueCode);
		this.productIssueDao.insert(aProductIssue.convertTo());
	}

	/**
	 * $method.remarks
	 * 
	 * @param aProductIssue
	 * @throws Exception
	 */
	protected void doModifyProductIssue(final com.ourteam.product.requirement.domain.ProductIssueBean aProductIssue)
			throws Exception {

		this.productIssueDao.update(aProductIssue.convertTo());
	}

	/**
	 * $method.remarks
	 * 
	 * @param aProductIssueIds
	 * @throws Exception
	 */
	protected void doDeleteProductIssues(final long[] aProductIssueIds) throws Exception {

		if (ArrayUtils.isEmpty(aProductIssueIds)) {
			return;
		}

		ProductIssueAttachDAOQueryBean attachDAOQueryBean = new ProductIssueAttachDAOQueryBean();

		attachDAOQueryBean.createCriteria().andIssueIdIn(aProductIssueIds);

		this.productIssueAttachDao.delete(attachDAOQueryBean);

		ProductIssueNoteDAOQueryBean noteDAOQueryBean = new ProductIssueNoteDAOQueryBean();

		noteDAOQueryBean.createCriteria().andIssueIdIn(aProductIssueIds);

		this.productIssueNoteDao.delete(noteDAOQueryBean);

		ProductIssueDAOQueryBean queryBean = new ProductIssueDAOQueryBean();

		queryBean.createCriteria().andIdIn(aProductIssueIds);

		this.productIssueDao.delete(queryBean);

	}

	/**
	 * $method.remarks
	 * 
	 * @param aProductIssueId
	 * @throws Exception
	 */
	protected com.ourteam.product.requirement.domain.ProductIssueBean doGetProductIssueById(final long aProductIssueId)
			throws Exception {

		ProductIssue productIssue = this.productIssueDao.findById(aProductIssueId);

		return productIssue != null ? new ProductIssueBean(productIssue) : null;

	}

	/**
	 * $method.remarks
	 * 
	 * @param queryBean
	 * @throws Exception
	 */
	protected com.ourteam.product.requirement.domain.ProductIssueBean[] doQueryProductIssues(
			final com.ourteam.product.requirement.dao.ProductIssueDAOQueryBean queryBean) throws Exception {

		return ProductIssueBean.toArray(this.productIssueDao.query(queryBean));

	}

	/**
	 * $method.remarks
	 * 
	 * @param queryBean
	 * @throws Exception
	 */
	protected int doQueryProductIssueCount(final com.ourteam.product.requirement.dao.ProductIssueDAOQueryBean queryBean)
			throws Exception {

		return this.productIssueDao.queryCount(queryBean);

	}

	/**
	 * $method.remarks
	 * 
	 * @param aProductIssueAttach
	 * @throws Exception
	 */
	protected void doAddNewProductIssueAttach(
			final com.ourteam.product.requirement.domain.ProductIssueAttachBean aProductIssueAttach) throws Exception {

		this.productIssueAttachDao.insert(aProductIssueAttach.convertTo());
	}

	/**
	 * $method.remarks
	 * 
	 * @param aProductIssueAttach
	 * @throws Exception
	 */
	protected void doModifyProductIssueAttach(
			final com.ourteam.product.requirement.domain.ProductIssueAttachBean aProductIssueAttach) throws Exception {

		this.productIssueAttachDao.update(aProductIssueAttach.convertTo());
	}

	/**
	 * $method.remarks
	 * 
	 * @param aProductIssueAttachIds
	 * @throws Exception
	 */
	protected void doDeleteProductIssueAttachs(final long[] aProductIssueAttachIds) throws Exception {
		if (ArrayUtils.isEmpty(aProductIssueAttachIds)) {
			return;
		}

		ProductIssueAttachDAOQueryBean queryBean = new ProductIssueAttachDAOQueryBean();

		queryBean.createCriteria().andIdIn(aProductIssueAttachIds);

		this.productIssueAttachDao.delete(queryBean);
	}

	/**
	 * $method.remarks
	 * 
	 * @param aProductIssueAttachId
	 * @throws Exception
	 */
	protected com.ourteam.product.requirement.domain.ProductIssueAttachBean doGetProductIssueAttachById(
			final long aProductIssueAttachId) throws Exception {

		ProductIssueAttach issueAttach = this.productIssueAttachDao.findById(aProductIssueAttachId);

		return issueAttach != null ? new ProductIssueAttachBean(issueAttach) : null;

	}

	/**
	 * $method.remarks
	 * 
	 * @param queryBean
	 * @throws Exception
	 */
	protected com.ourteam.product.requirement.domain.ProductIssueAttachBean[] doQueryProductIssueAttachs(
			final com.ourteam.product.requirement.dao.ProductIssueAttachDAOQueryBean queryBean) throws Exception {

		return ProductIssueAttachBean.toArray(this.productIssueAttachDao.query(queryBean));
	}

	/**
	 * $method.remarks
	 * 
	 * @param queryBean
	 * @throws Exception
	 */
	protected int doQueryProductIssueAttachCount(
			final com.ourteam.product.requirement.dao.ProductIssueAttachDAOQueryBean queryBean) throws Exception {

		return this.productIssueAttachDao.queryCount(queryBean);
	}

	/**
	 * $method.remarks
	 * 
	 * @param aProductIssueNote
	 * @throws Exception
	 */
	protected void doAddNewProductIssueNote(
			final com.ourteam.product.requirement.domain.ProductIssueNoteBean aProductIssueNote) throws Exception {

		this.productIssueNoteDao.insert(aProductIssueNote.convertTo());

	}

	/**
	 * $method.remarks
	 * 
	 * @param aProductIssueNote
	 * @throws Exception
	 */
	protected void doModifyProductIssueNote(
			final com.ourteam.product.requirement.domain.ProductIssueNoteBean aProductIssueNote) throws Exception {
		this.productIssueNoteDao.update(aProductIssueNote.convertTo());
	}

	/**
	 * $method.remarks
	 * 
	 * @param aProductIssueNoteIds
	 * @throws Exception
	 */
	protected void doDeleteProductIssueNotes(final long[] aProductIssueNoteIds) throws Exception {

		if (ArrayUtils.isEmpty(aProductIssueNoteIds)) {
			return;
		}

		ProductIssueNoteDAOQueryBean queryBean = new ProductIssueNoteDAOQueryBean();

		queryBean.createCriteria().andIdIn(aProductIssueNoteIds);

		this.productIssueNoteDao.delete(queryBean);

	}

	/**
	 * $method.remarks
	 * 
	 * @param aProductIssueNoteId
	 * @throws Exception
	 */
	protected com.ourteam.product.requirement.domain.ProductIssueNoteBean doGetProductIssueNoteById(
			final long aProductIssueNoteId) throws Exception {

		ProductIssueNote issueNote = this.productIssueNoteDao.findById(aProductIssueNoteId);

		return issueNote != null ? new ProductIssueNoteBean(issueNote) : null;
	}

	/**
	 * $method.remarks
	 * 
	 * @param queryBean
	 * @throws Exception
	 */
	protected com.ourteam.product.requirement.domain.ProductIssueNoteBean[] doQueryProductIssueNotes(
			final com.ourteam.product.requirement.dao.ProductIssueNoteDAOQueryBean queryBean) throws Exception {

		return ProductIssueNoteBean.toArray(this.productIssueNoteDao.query(queryBean));
	}

	/**
	 * $method.remarks
	 * 
	 * @param queryBean
	 * @throws Exception
	 */
	protected int doQueryProductIssueNoteCount(
			final com.ourteam.product.requirement.dao.ProductIssueNoteDAOQueryBean queryBean) throws Exception {

		return this.productIssueNoteDao.queryCount(queryBean);
	}

	@Override
	protected Map<String, Object> doQueryProductIssuesStatistics(ProductIssueDAOQueryBean queryBean) throws Exception {

		if (queryBean == null) {
			queryBean = new ProductIssueDAOQueryBean();
		}

		queryBean.addCountFunctionProperty(IProductIssueDAO.Id, "count");

		queryBean.addStatusSelectProperty();

		queryBean.setGroupProperty(IProductIssueDAO.Status);

		Map<String, Object>[] results = this.productIssueDao.querySelective(queryBean);

		Map<String, Object> result = new HashMap<>();

		for (int i = 0; i < results.length; i++) {

			result.put(MapUtils.getString(results[i], IProductIssueDAO.DataProperty.Status),
					MapUtils.getInteger(results[i], "count"));
		}

		return result;
	}

	@Override
	protected void doAddNewProductIssueChangeLog(ProductIssueChangeLogBean aProductIssueChangeLog) throws Exception {

		aProductIssueChangeLog.setOperateTime(new Date());
		this.productIssueChangeLogDao.insert(aProductIssueChangeLog.convertTo());

	}

	@Override
	protected void doModifyProductIssueChangeLog(ProductIssueChangeLogBean aProductIssueChangeLog) throws Exception {
		this.productIssueChangeLogDao.update(aProductIssueChangeLog.convertTo());
	}

	@Override
	protected void doDeleteProductIssueChangeLogs(long[] aProductIssueChangeLogIds) throws Exception {
		if (ArrayUtils.isEmpty(aProductIssueChangeLogIds)) {
			return;
		}

		ProductIssueChangeLogDAOQueryBean queryBean = new ProductIssueChangeLogDAOQueryBean();

		queryBean.createCriteria().andIdIn(aProductIssueChangeLogIds);

		this.productIssueChangeLogDao.delete(queryBean);

	}

	@Override
	protected ProductIssueChangeLogBean doGetProductIssueChangeLogById(long aProductIssueChangeLogId) throws Exception {

		ProductIssueChangeLog changeLog = this.productIssueChangeLogDao.findById(aProductIssueChangeLogId);

		return changeLog != null ? new ProductIssueChangeLogBean(changeLog) : null;
	}

	@Override
	protected ProductIssueChangeLogBean[] doQueryProductIssueChangeLogs(ProductIssueChangeLogDAOQueryBean queryBean)
			throws Exception {
		return ProductIssueChangeLogBean.toArray(this.productIssueChangeLogDao.query(queryBean));
	}

	@Override
	protected int doQueryProductIssueChangeLogCount(ProductIssueChangeLogDAOQueryBean queryBean) throws Exception {
		return this.productIssueChangeLogDao.queryCount(queryBean);
	}

}
